﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Modbus.Net8.Commom
{
    public static class DataExtensions
    {
        /// <summary>
        /// 将byte加入List<byte>
        /// ASCII模式
        /// byte转为大写的16进制字符串，List依次添加ASCII码的byte值
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="data"></param>
        public static void SetByteAscii(this List<byte> bytes, byte data)
        {
            string str = data.ToString("X2");
            foreach (char s in str)
                bytes.Add((byte)s);
        }

        /// <summary>
        /// 获取byte ASCII模式
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="idx"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static byte GetByteAscii(this byte[] bytes, ref int idx)
        {
            if (idx + 2 > bytes.Length)
                throw new Exception("GetByteAscii方法访问bytes，索引超出范围");

            return (byte)(bytes[idx++].AsciiToInt() * 16 + bytes[idx++].AsciiToInt());
        }

        /// <summary>
        /// 16进制的byte 转换为int值
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        static int AsciiToInt(this byte data)
        {
            int result = data - '0';
            if (result >= 49)   //'a'-'f'
                result -= 39;
            if (result >= 17)   //'A'-'F'
                result -= 7;
            return result;
        }

        /// <summary>
        /// 获取len长度的byte数组 ASCII模式
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="idx"></param>
        /// <param name="len"></param>
        /// <returns></returns>
        /// <exception cref="Exception"></exception>
        public static byte[] GetByteArrAscii(this byte[] bytes, ref int idx, int len)
        {
            if (idx + len > bytes.Length)
                throw new Exception("GetByteAscii方法访问bytes，索引超出范围");

            byte[] arr = new byte[len];
            for (int i = 0; i < len; i++)
                arr[i] = bytes.GetByteAscii(ref idx);

            return arr;
        }

        /// <summary>
        /// 将ushort加入List<byte>
        /// </summary>
        /// <param name="bytes"></param>
        /// <param name="data"></param>
        public static void SetUshort(this List<byte> bytes, ushort data)
        {
            bytes.Add((byte)(data >> 8));    //高位
            bytes.Add((byte)(data & 0xff));  //低位
        }

        public static ushort GetUshort(this byte[] bytes, int idx)
        {
            if (idx + 1 >= bytes.Length)
                throw new Exception($"GetUshort方法访问bytes，索引超出范围");
            return (ushort)(bytes[idx] * 256 + bytes[idx + 1]);
        }

        public static ushort GetUshort(this byte[] bytes, ref int idx)
        {
            if (idx + 1 >= bytes.Length)
                throw new Exception($"GetUshort方法访问bytes，索引超出范围");
            return (ushort)(bytes[idx++] * 256 + bytes[idx++]);
        }

    }
}
